home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12640 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  54 lines

  1. Path: druid.borland.com!usenet
  2. From: pete@borland.com (Pete Becker)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: extern consts
  5. Date: 20 Mar 1996 23:18:32 GMT
  6. Organization: Borland International
  7. Message-ID: <4iq3o8$rdh@druid.borland.com>
  8. References: <4idbcv$ue2@news7.erols.com> <314c2077.138956468@nntp.ix.netcom.com> <4ii7h9$bq6@news6.erols.com> <4iin9h$fdk@clarknet.clark.net>
  9. NNTP-Posting-Host: pbecker.borland.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=ISO-8859-1
  12. X-Newsreader: WinVN 0.99.5
  13.  
  14. In article <4iin9h$fdk@clarknet.clark.net>, gusty@clark.net says...
  15. >
  16. >Chris Cobb (ccobb@cseg.com) wrote:
  17. >: miker3@ix.netcom.com (Mike Rubenstein) wrote:
  18. >: >
  19. >: >It's not legal.  extern const is legal, but in any compilation unit
  20. >: >that does not see the initializer the const variable is not a constant
  21. >: >expression.
  22. >: >
  23. >: 
  24. >: Well, your comment makes sense.  However, in a way it seem 
  25. >: self-contradictory.  If a const can be extern, then it no longer is a 
  26. >: const: you've basically externed away constness.  
  27. >
  28. >Huh? The word "const" means, fundamentally, "cannot be changed once 
  29. >initialized". This holds just as true for an externally defined const as 
  30. >any other. If you try the following in a module:
  31. >
  32. >        extern const int iConst;
  33. >        iConst = 5;
  34. >
  35. >it won't compile. So what do you mean by, "it no longer is a const"?
  36. >
  37.  
  38. The confusion here is over the difference between a const object, that is, an 
  39. object that cannot be changed once initialized, and a constant expression, that 
  40. is, an expression that can be treated as a constant at compile time. For 
  41. example:
  42.  
  43. extern const size1;
  44. const size2 = 3;
  45.  
  46. int array1[size1];    // illegal: size1 is not a constant expression
  47. int array2[size2];    // legal
  48.  
  49. Both size1 and size2 are constants, 'cause that's the way they're declared. But 
  50. size1 is not a "constant expression" in the sense that the language definition 
  51. uses the term, because the compiler doesn't know what its value is.
  52.     -- Pete
  53.  
  54.